package com.example.task.sqlite.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.task.bean.User;
import com.example.task.sqlite.DBHelper;

public class UserDao {
    private DBHelper dbHelper;

    public UserDao(Context context) {
        this.dbHelper = new DBHelper(context);
    }

    public void add(User u){
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", u.getIduser());
        contentValues.put("username", u.getUsername());
        contentValues.put("password", u.getPassword());
        contentValues.put("name", u.getName());
        contentValues.put("email", u.getEmail());
        contentValues.put("telephone", u.getTelephone());
        contentValues.put("birthday", u.getBirthday());
        contentValues.put("sex", u.getSex());
        contentValues.put("initdate", u.getInitdate());
        contentValues.put("classRoom", u.getClassRoom());
        contentValues.put("faculty", u.getFaculty());
        contentValues.put("imgpath", u.getImgpath());

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.insert("user", null, contentValues);
    }
    public void delete(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.execSQL("DELETE FROM user");
    }
    public Cursor query(){
        String sql = "select iduser as _id, id, username, password, name, email, telephone, birthday,sex,initdate,classRoom,faculty,imgpath from user";

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, null);

//        List<User> list = new ArrayList();
//        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
//            int nameColumn = cursor.getColumnIndex(User.name_User);
//            int phoneColumn = cursor.getColumnIndex(User.telephone_User);
//            int emailColumn = cursor.getColumnIndex(User.email_User);
//            String name = cursor.getString(nameColumn);
//            String telephone = cursor.getString(phoneColumn);
//            String email = cursor.getString(emailColumn);
//
//            User user = new User();
//            user.setUsername(name);
//            user.setTelephone(telephone);
//            user.setEmail(email);
//            list.add(user);
//        }
//
//        Log.i("query",list.toString());

        return cursor;
    }
    public User query1(String username1){
        String sql = "select iduser as _id, id, username, password, name, email, telephone, birthday,sex,initdate,classRoom,faculty,imgpath from user";
        User user;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, null);
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            int nameColumn = cursor.getColumnIndex(User.name_User);
            int phoneColumn = cursor.getColumnIndex(User.telephone_User);
            int emailColumn = cursor.getColumnIndex(User.email_User);

            String iduser = cursor.getString(cursor.getColumnIndex("id"));
            String password = cursor.getString(cursor.getColumnIndex("password"));//学生密码
            String name = cursor.getString(cursor.getColumnIndex("name"));;//学生名字
            String birthday = cursor.getString(cursor.getColumnIndex("birthday"));;//注册时间
            String sex = cursor.getString(cursor.getColumnIndex("sex"));;//性别
            String initdate = cursor.getString(cursor.getColumnIndex("initdate"));;
            String classRoom = cursor.getString(cursor.getColumnIndex("classRoom"));;//学生班别
            String faculty = cursor.getString(cursor.getColumnIndex("faculty"));;//学生院系
            String imgpath = cursor.getString(cursor.getColumnIndex("imgpath"));;//头像
            String username = cursor.getString(nameColumn);
            String telephone = cursor.getString(phoneColumn);
            String email = cursor.getString(emailColumn);
            if(username.equals(username1)){
                System.out.println("Username:"+username+","+iduser);
                user = new User(Integer.parseInt(iduser),username, password, name, email, telephone, birthday, sex, initdate, classRoom, faculty, imgpath);
                return user;
            }
        }
        return null;
    }
}
